package com.muyu.danmo.service.impl;


import com.muyu.common.core.domain.Result;
import com.muyu.common.core.utils.DateUtils;
import com.muyu.danmo.domain.DanmoMountFavorite;
import com.muyu.danmo.domain.DanmoPrintingFavorite;
import com.muyu.danmo.mapper.DanmoMountFavoriteMapper;
import com.muyu.danmo.mapper.DanmoMountMessageMapper;
import com.muyu.danmo.service.IDanmoMountFavoriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 装裱收藏Service业务层处理
 *
 * @author kunzong
 * @date 2024-12-05
 */
@Service
public class DanmoMountFavoriteServiceImpl implements IDanmoMountFavoriteService {
    @Autowired
    private DanmoMountFavoriteMapper danmoMountFavoriteMapper;

    @Autowired
    private DanmoMountMessageMapper danmoMountMessageMapper;

    /**
     * 查询装裱收藏
     *
     * @param id 装裱收藏主键
     * @return 装裱收藏
     */
    @Override
    public DanmoMountFavorite selectDanmoMountFavoriteById(Long id) {
        return danmoMountFavoriteMapper.selectDanmoMountFavoriteById(id);
    }

    /**
     * 查询装裱收藏列表
     *
     * @param userId 装用户ID
     * @return 装裱收藏
     */
    @Override
    public List<DanmoMountFavorite> selectDanmoMountFavoriteList(Long userId) {
        return danmoMountMessageMapper.selectDanmoMountByFavoriteList(userId);
    }

    /**
     * 新增装裱收藏
     *
     * @param id 装裱ID
     * @param userId 用户ID
     * @return 结果
     */
    @Override
    public Result insertDanmoMountFavorite(Long id, Long userId) {
        if (danmoMountFavoriteMapper.existsByUserIdAndPrintingClassId(userId, id)) {
            return Result.success(1, "用户已收藏");
        }
        DanmoMountFavorite danmoMountFavorite = new DanmoMountFavorite();
        danmoMountFavorite.setUserId(userId);
        danmoMountFavorite.setProductId(id);
        danmoMountFavorite.setCreateBy(String.valueOf(userId));
        danmoMountFavorite.setCreateTime(DateUtils.getNowDate());
        danmoMountFavoriteMapper.insertDanmoMountFavorite(danmoMountFavorite);
        return Result.success(1, "收藏成功");
    }

    /**
     * 修改装裱收藏
     *
     * @param danmoMountFavorite 装裱收藏
     * @return 结果
     */
    @Override
    public int updateDanmoMountFavorite(DanmoMountFavorite danmoMountFavorite) {
        danmoMountFavorite.setUpdateTime(DateUtils.getNowDate());
        return danmoMountFavoriteMapper.updateDanmoMountFavorite(danmoMountFavorite);
    }

    /**
     * 批量删除装裱收藏
     *
     * @param ids 需要删除的装裱收藏主键
     * @return 结果
     */
    @Override
    public Result deleteDanmoMountFavoriteByIds(Long userId ,Long[] ids) {
        for (Long id : ids) {
            if (!danmoMountFavoriteMapper.existsByUserIdAndPrintingClassId(userId, id)) {
                return Result.error("取消失败");
            }
        }
        danmoMountFavoriteMapper.deleteDanmoMountFavoriteByIds(ids, userId);
        return Result.success(1,"取消成功");
    }

    @Override
    public int deleteDanmoMountFavoriteById(Long id) {
        return 0;
    }


}
